<template>
  <base-breadcrumb :title="$route.meta.title">
  <!-- 现场质量检查 -->
    <div>
      <!-- 搜索 -->
      <searchData :searchData="searchArr" @search="search"></searchData>
      <!-- 表格 -->
      <a-card :bordered="false">
        <a-tabs default-active-key="1" @change="handleTabChange">
          <a-tab-pane :key="index" :tab="item.name"  v-for="(item, index) in categoryArr"></a-tab-pane>
        </a-tabs>
        <!-- 汇总 -->
        <collapseStatic
          :columnsSummary="columnsSummary"
          :tabList="tabList"
          :form="queryParam"
          :loadCount="loadCount"
          :loadDataSummaryApi="api"
          :notExpand="true"
        >
          <template #left="scoped">
            <span
              >检查记录数
              <a
                ><b>{{ scoped.data.total || 0 }}</b></a
              >，</span
            >
            <span
              >合格数
              <a
                ><b>{{ scoped.data.qualifiedNum || 0 }}</b></a
              >，</span
            >
            <span
              >不合格数
              <a
                ><b>{{ scoped.data.unQualifiedNum || 0 }}</b></a
              >，</span
            >
            <span
              >合格率
              <a
                ><b>{{ scoped.data.qualifiedRate || '0%' }}</b></a
              ></span
            >
          </template>
        </collapseStatic>
        <list-table
          ref="table"
          tableLayout="fixed"
          size="default"
          rowKey="id"
          :columns="columns"
          :request="loadData"
          :alert="true"
          show-size-changer
          show-quick-jumper
          showPagination="auto"
        >
          <span slot="action" slot-scope="text, record">
            <template>
              <!-- 查看关联BIM模型 -->
              <a-tooltip>
                <template slot="title">{{ `${record.modelId ? '已' : '未'}关联BIM模型` }}</template>
                <a-icon @click.stop="handleRelease(record)" type="link" :style="{ color: record.modelId ? '#1890ff' : '#ccc' }" />
              </a-tooltip>
              <a class="ml12" v-btnPermission="'qm_003_loo_04'" @click="handleManage(record, 0)">查看</a>
              <!-- <a-divider v-btnPermission="'qm_003_edi_02'" type="vertical"></a-divider>
                  <a v-btnPermission="'qm_003_edi_02'" @click="handleManage(record, 1)">编辑</a>
                  <a-divider v-btnPermission="'qm_003_del_03'" type="vertical"></a-divider>
                  <a v-btnPermission="'qm_003_del_03'" class="del_btn" @click="handleDelete(record)">删除</a> -->
            </template>
          </span>
        </list-table>
      </a-card>
    </div>
    <!-- 关联BIM模型弹窗 -->
  <BimRelease ref="bimRelease" :zIndex="950" :destroyOnClose="true" :isRelease="false" :visible.sync="visible" :modelList="modelList" />
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格
import { checkRecord } from '@/api/quality/quality' // api
import CollapseStatic from '@/views/quality/components/CollapseStatic' // 汇总
import OrgTreeSelect from '@/components/OrgTreeSelect'
import Selector from '@/components/Select/Selector' // 检查类型
import SelectProjectType from '@/components/Select/SelectProjectType' // 工程类别
import SelectProjectStatus from '@/views/quality/components/SelectProjectStatus' // 项目状态
import BimRelease from '@/components/Dialog/BimRelease' // BIM模型关联
import { typesLabel } from '@/api/quality/quality' // api

const searchArr = [
  // 搜索框
  {
    name: '受检单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  {
    name: '项目状态',
    type: SelectProjectStatus,
    valueKey: 'projectStatus',
  },
  {
    name: '工程类别',
    type: SelectProjectType,
    valueKey: 'constructionTypeIds',
    showStop:false
  },
  {
    name: '分项工程',
    value: 'subProject',
    type: 'input'
  },
  {
    name: '检查结果',
    valueKey: 'checkResult',
    type: Selector,
    selectType: '2'
  },
  {
    name: '检查类型',
    valueKey: 'checkType',
    type: Selector,
    selectType: '0',
    attr: { code: '1' }
  },
  {
    name: '检查人',
    value: 'checkPeople',
    type: 'input'
  },
  {
    name: '检查时间',
    value: 'checkTime',
    type: 'dateRange',
    attr: {
      rangeFormat: ['checkTimeStart', 'checkTimeEnd']
    }
  },
  {
    name: '质量检查编号',
    value: 'checkNumber',
    type: 'input'
  },
  {
    name: '来源',
    valueKey: 'sourceId',
    type: Selector,
    selectType: '57'
  },
]
const columns = [
  {
    title: '受检单位',
    dataIndex: 'unitName',
    ellipsis: true,
    width: 150
  },
  // {
  //   title: '项目名称',
  //   dataIndex: 'projectName',
  //   ellipsis: true,
  //    width: 150
  // },
  {
    title: '质量检查编号',
    dataIndex: 'checkNumber',
    ellipsis: true,
     width: 150
  },
  {
    title: '检查结果',
    width: 100,
    dataIndex: 'checkResult'
  },
  {
    title: '分项工程',
    ellipsis: true,
    width: 150,
    dataIndex: 'subProject'
  },
  {
    title: '检查人',
    ellipsis: true,
     width: 150,
    dataIndex: 'checkPeople'
  },
  {
    title: '检查类型',
     width: 150,
    dataIndex: 'checkType'
  },
  {
    title: '检查时间',
    dataIndex: 'checkTime',
    width: 120
  },
  {
    title: '来源',
    dataIndex: 'sourceName',
    width: 120
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 150,
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'a' + Date.now(),
  components: {
    STable,
    CollapseStatic,
    BimRelease
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    return {
      queryParam: {}, // 查询参数
      loading: false, // 详情加载控件
      visible: false,
      modelList: [],
      columnsSummary: [
        {
          title: '检查类型',
          dataIndex: 'name',
          ellipsis: true
        },
        {
          title: '检查总数',
          dataIndex: 'total',
          ellipsis: true
        },
        {
          title: '合格数',
          dataIndex: 'qualifiedNum'
        },
        {
          title: '不合格数',
          dataIndex: 'unQualifiedNum'
        },
        {
          title: '合格率',
          dataIndex: 'qualifiedRate'
        }
      ], // 汇总表头
      tabList: [
        {
          key: '-1',
          name: '全部'
        },
        {
          key: '0',
          name: '本周'
        },
        {
          key: '1',
          name: '本月'
        },
        {
          key: '2',
          name: '本年'
        }
      ], // 汇总标题
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return checkRecord.postList(requestParameters).then((res) => {
          return res
        })
      },
      loadCount: 0,
      api: checkRecord,
      categoryArr: [], //整改状态
      checkTypeId:null,
    }
  },
  created() {
    typesLabel.getList({ type: 56 }).then((res) => {
      if (res.code === 200) {
        this.categoryArr = [{ name: '全部' }]
        this.categoryArr = this.categoryArr.concat(res.data)
      }
    })
  },
  methods: {
    handleTabChange(key) {
      this.queryParam.checkTypeId = this.categoryArr[key].id
      this.checkTypeId = this.categoryArr[key].id;
      this.$refs.table.refresh(true)
      this.loadCount++
    },
    /**
     * 搜索
     */
    search(value) {
      if (value) {
        this.queryParam = value
      }
      this.queryParam.checkTypeId = this.checkTypeId
      this.$refs.table.refresh(true)
      this.loadCount++
    },
    /**
     * 操作栏
     */
    handleManage(record, type) {
      this.$router.push({
        name: 'newRecordInfo',
        query: {
          id: record?.id,
          qualityInspectionRecord:true
        }
      })
    },
    /**
     * @description: 查看关联的BIM模型
     * @param {*} record：业务对象
     * @return {*}
     */
    handleRelease(record) {
      if (record.modelId) {
        this.modelList = [{ id: record.modelId, modelId: record.modelFileId, version: record.modelFileVersion, modelTitle: record.modelTitle, uuId: [record.bimId?.split('&&&&')?.[1]], subAreaId: record.subAreaId }]
        this.visible = true
      }
    },
    /**
     * 删除记录
     */
    handleDelete(e) {
      let that = this
      that.$confirm({
        title: '温馨提示',
        content: '确认删除？',
        okText: '确认',
        cancelText: '关闭',
        onOk() {
          return checkRecord
            .remove({
              id: e.id
            })
            .then((res) => {
              that.$message.success('删除成功')
              // 重置页面
              that.$refs.table.loadData()
            })
          // .catch((res) => {
          //   that.$message.warning(res?.data?.message || '删除失败！')
          // })
        }
      })
    }
  }
}
</script>


